package com.woai.vichat.nearby.frgm;
import com.woai.vichat.R;
import android.view.View;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.woai.vichat.base.frgm.BaseFragment;
import com.woai.vichat.nearby.adapter.NearbyListAdapter;
import com.woai.vichat.nearby.entity.NearbyListModel;
import com.woai.vichat.nearby.presenter.NearbyListPresenter;
import com.woai.common.util.IntentUtils;
import com.woai.common.util.ToastUtil;

/**
 * NearbyListFrgm
 * Created on 2018-06-21 14:22:56.
 * @author WuGefei
 */
public class NearbyListFrgm extends BaseFragment<NearbyListFrgm, NearbyListPresenter> implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener{

    @BindView(R.id.titleBar)
    RelativeLayout titleBar;

    @BindView(R.id.tvTopTitle)
    TextView tvTitle;

    @BindView(R.id.iv_back)
    ImageView ivBack;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    private int currentPos = 1;

    private NearbyListAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.frgm_nearby_list;
    }
	
    @Override
    protected void init(View rootView) {
        ButterKnife.bind(this, rootView);
        titleBar.setVisibility(View.VISIBLE);
        tvTitle.setText("NearbyList");
        initAdapter();
        request();
    }

    private void request(){
        presenter.request(currentPos, getActivity());
    }

    private void initAdapter(){
        mAdapter = new NearbyListAdapter(R.layout.item_nearby_list, null);
        mAdapter.setEmptyView(R.layout.empty_view, (ViewGroup) recyclerView.getParent());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(this, recyclerView);
        swipeRefresh.setOnRefreshListener(this);
    }

    public void bindData(NearbyListModel model){
        mAdapter.loadMoreComplete();
        swipeRefresh.setRefreshing(false);
        if (model.getCurrentPage() == 1) {
            mAdapter.setNewData(model.getData());
        } else {
            if (model.getData().size() > 0) {
                mAdapter.addData(model.getData());
            }
        }

        if(model==null){

        }

        //if (currentPos >= data.getPageTotal()) {
            //第一页如果不够一页就不显示没有更多数据布局
        //    mAdapter.loadMoreEnd(false);
        //}
    }

	@Override
    public void onRefresh() {
        currentPos = 1;
        request();
        mAdapter.setEnableLoadMore(false);
    }

    @Override
    public void onLoadMoreRequested() {
        currentPos++;
        request();
    }

    @Override
    protected NearbyListPresenter createPresenter() {
        return new NearbyListPresenter();
    }
}